﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace wf23_9FuncYield
{
    public partial class Form1 : Form
    {
        Func<int, string> deIntStr; //Func== predefined generic delegate
        Func<string, string> deStrStr;

        string[] tNames = { "Agatha", "Cecily", "Martha" };

        public Form1()
        {
            InitializeComponent();
        }//ctor.

        string Meth1pX(int x)
        {
            return (1 + x).ToString();
        }//.

        private void BDelMeth_Click(object sender, EventArgs e)
        {
            deIntStr = Meth1pX; //explicit definition of the delegate via a method 
            TxBY1.Text = TxBY1.Text.Length <= 0 ? deIntStr((int)NUD1.Value) : "";
        }//.

        private void BDelAnony_Click(object sender, EventArgs e)
        {
            deIntStr = delegate(int x) { return (2 * x).ToString(); }; //anonymous meth
            TxBY2.Text = TxBY2.Text.Length <= 0 ? deIntStr((int)NUD2.Value) : "";
        }//.

        private void BDelLamb_Click(object sender, EventArgs e)
        {
            deIntStr = x => (3 * x).ToString(); //delegate with Lambda expression
            TxBY3.Text = TxBY3.Text.Length <= 0 ? deIntStr((int)NUD3.Value) : "";
        }//.

        private void BDelLambMeth_Click(object sender, EventArgs e)
        {
            deIntStr = x => Meth1pX(x); //Lambda expression uses Meth1pX
            TxBY4.Text = TxBY4.Text.Length <= 0 ? deIntStr((int)NUD4.Value) : "";
        }

        //RIGHT side of Form1:

        string MySelect(string s)
        {
            return s.Substring(0, 1) + ", ";
        }//.

        private void BFunc1_Click(object sender, EventArgs e)
        {
            deStrStr = MySelect; //delegate wraps a named method
            string s = tNames.Select<string, string>(deStrStr)
                            .Aggregate("", (x, y) => x += y); //()=> ==ACCUMULATOR fu
            TxBFunc1.Text = TxBFunc1.Text.Length <= 0 ?
                                  s.Substring(0, s.Length - 2) : "";//rem last ", "
        }//.

        private void BFunc2_Click(object sender, EventArgs e)
        {
            deStrStr = x => x.Substring(0, 2) + ", "; //delegate wraps an anonymous method
            string s = tNames.Select(deStrStr) //standard query op infers string types
                            .Aggregate("", (x, y) => x += y); //"" ==seed 
            TxBFunc2.Text = TxBFunc2.Text.Length <= 0 ?
                                  s.Substring(0, s.Length - 2) : "";
        }//.


        private void BFunc3_Click(object sender, EventArgs e)
        {
            string s = tNames.Select(x => x.Substring(0, 3) + ", ")
                            .Aggregate("", (x, y) => x += y);
            TxBFunc3.Text = TxBFunc3.Text.Length <= 0 ?
                                  s.Substring(0, s.Length - 2) : "";
        }//.

        private void BFunc4_Click(object sender, EventArgs e)
        {
            TxBFunc4.Text = TxBFunc4.Text.Length <= 0 ? tNames.Select(x => x + "\r\n")
                                                           .Where(x => x.ToUpper().Contains('A'))
                                                           .Aggregate("", (x, y) => x += y) : "";
        }//.

        //BOTTOM of Form1:

        IEnumerable<string> MyYieldStr()
        {
            yield return "Building, ";
            yield return "Pavilion, ";
            yield return "Edifice";
        }//.

        private void BYield_Click(object sender, EventArgs e)
        {
            TxBYield.Text = TxBYield.Text.Length <= 0 ?
                           MyYieldStr().Aggregate("", (x, y) => x += y) : "";
        }

    }
}
